/*
 * @lc app=leetcode.cn id=74 lang=cpp
 *
 * [74] 搜索二维矩阵
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>

using namespace std;
// @lc code=start
class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int m = matrix.size();
        int n = matrix[0].size();
        int length = m * n;
        int l = 0,r = length -1;
        while(l <= r) {
            int mid =  (l + r)/2;
            if(matrix[mid/n][mid%n] == target) {
                return true;
            }else if(matrix[mid/n][mid%n] > target) {
                r = mid - 1;
            }else {
                l = mid + 1;
            }
        }
        return false;
    }
};
// @lc code=end

